package cn.tedu.submarine;
import java.util.Random;
import javax.swing.ImageIcon;
import  java.awt.Graphics;

// 海洋对象
public abstract class SeaObject {
    public static final int LIVE = 0; //活着
    public static final int DEAD = 1; //死了
    protected int state = LIVE; //当前状态，默认状态为活着的

    protected int width;
    protected int height;
    protected int x;
    protected int y;
    protected int speed;

    // 三种潜艇的x/y/speed是一样的，所以数据直接写死，无需传参
    public SeaObject(int width, int height){
        this.width = width;
        this.height = height;
        x = -width;
        Random rand = new Random();
        y = rand.nextInt(World.HEIGHT-height-150+1) + 150;
        speed = rand.nextInt(3) + 1;
    }
    //    战舰，鱼类，水雷，深水炸弹的x/y/speed不能写死
    public SeaObject(int width, int height, int x, int y, int speed){
        this.width = width;
        this.height = height;
        this.x = x;
        this.y = y;
        this.speed = speed;
    }
    // 海洋对象移动
    public abstract void step();
    // 获取图片
    public abstract ImageIcon getImage();
    // 判断是否活着
    public boolean isLive(){
        return state==LIVE;
    }
    // 判断是否死了
    public boolean isDead(){
        return state==DEAD;
    }
    // 画页面
    public void paintImage(Graphics g){
        // 如果活着才画
        if(this.isLive()){
            this.getImage().paintIcon(null, g, this.x, this.y);
        }
    }

}
